package com.wzq.solution;

import java.util.*;

class Solution1090 {
    public static int largestValsFromLabels(int[] values, int[] labels, int num_wanted, int use_limit) {

        Map<Integer, LinkedList<Integer>> m = new HashMap<Integer, LinkedList<Integer>>();
        for (int i=0;i<labels.length;i++){
            if(!m.containsKey(labels[i])){
                LinkedList<Integer> tmp = new LinkedList<Integer>();
                tmp.add(-Integer.MAX_VALUE);
                m.put(labels[i],tmp);
            }
            LinkedList<Integer> l = m.get(labels[i]);

            for (int j=0;j<l.size();j++){
                if(values[i]>l.get(j)){
                    l.add(j,values[i]);
                    break;
                }
            }
        }

        LinkedList<Integer> t = new LinkedList<Integer>();
        t.add(-Integer.MAX_VALUE);

        for (Integer mk:m.keySet()){
            LinkedList<Integer> list = m.get(mk);
            for (int i=0;i<use_limit && i<list.size()-1;i++){

                for (int j=0;j<t.size();j++){
                    if(list.get(i)>t.get(j)){
                        t.add(j,list.get(i));
                        break;
                    }
                }
            }
        }

        int sum=0;
        for (int i=0;i<num_wanted && i<t.size()-1;i++){
            sum += t.get(i);
        }
        return sum;

    }


    public static int largestValsFromLabels2(int[] values, int[] labels, int num_wanted, int use_limit) {
        VL[] vl = new VL[values.length];
        for (int i=0;i<values.length;i++){
            vl[i] = new VL(values[i],labels[i]);
        }
        Arrays.sort(vl);
        VL[] newVl = new VL[values.length];
        int nowLabel = -1;
        int num_wanteds = 0;
        for (int i=0;i<vl.length;i++){
            if(nowLabel != vl[i].label){
                if(num_wanteds++ < num_wanted){

                }
            }
        }
        return 0;
    }

    public static void main(String[] args) {
        int[] values = new int[]{5,2,3,7,8};
        int[] labels = new int[]{1,0,1,1,2};

        int num_wanted=5;
        int use_limit=1;
        largestValsFromLabels2(values, labels, num_wanted,  use_limit);
    }

}

class VL implements Comparable<VL> {
    int value;
    int label;
    public VL(){

    }

    public VL(int value,int label){
        this.value = value;
        this.label = label;
    }



    @Override
    public int compareTo(VL o) {
        if(this.label != o.label){
            if(this.label>o.label){
                return 1;
            }else{
                return -1;
            }

        }else{
            if(this.value == o.value){
                return 0;
            }else if(this.value>o.value){
                return -1;
            }else {
                return 1;
            }
        }
    }
}